/**
 * 
 */
package systole.view.crud.patient.patientComponents.pathologies;

import java.awt.Frame;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import systole.domain.clinicalInformation.Pathology;
import systole.domain.clinicalInformation.PathologyPatient;
import systole.domain.clinicalInformation.PathologyPatientId;

import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.view.crud.patient.patientComponents.controller.ControllerEditionPatienComponent;
import systole.view.messages.EntityDescriptions;

/**
 * @author jmj
 *
 */
public class ControllerEditionPathologyByPatient extends ControllerEditionPatienComponent {

    /**
     * @param patient
     */
    public ControllerEditionPathologyByPatient(Patient patient) {
        super(patient);
        this.entityName = EntityDescriptions.PATHOLOGY;
        this.newDescription = "Nueva";
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        JDialogPathologyByPatient form = (JDialogPathologyByPatient) this.editForm;
        PathologyPatient pathologyPatient = (PathologyPatient) this.curretnEntity;
        pathologyPatient.setPathology((Pathology) form.getCmbPathologies().getSelectedItem());
        pathologyPatient.setInherited(form.getChkInherited().isSelected());
        pathologyPatient.setCurrently(form.getChkPresentToday().isSelected());
        Integer age = (Integer) form.getjSpnYears().getValue();
       pathologyPatient.setAgeAtStartAsInteger(age);
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        JDialogPathologyByPatient form = (JDialogPathologyByPatient) this.editForm;
        PathologyPatient pathologyPatient = (PathologyPatient) this.curretnEntity;
        Pathology pathology = pathologyPatient.getPathology();
        form.getCmbPathologies().setSelectedItem(pathology);
        form.getChkInherited().setSelected((pathologyPatient.getInherited() == null) ? false : pathologyPatient.getInherited());
        form.getChkPresentToday().setSelected((pathologyPatient.getCurrently() == null) ? false : pathologyPatient.getCurrently());
        form.getjSpnYears().setValue(pathologyPatient.getAgeAtStartAsInteger());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#save()
     */
    @Override
    protected void save() {
        if (!this.isEditing()) {
            PathologyPatient pathologyPatient = (PathologyPatient) this.curretnEntity;
            pathologyPatient.setId(new PathologyPatientId(pathologyPatient.getPathology().getId(), pathologyPatient.getPatient().getId()));
            this.patient.getPathologiesPatient().add(pathologyPatient);
        }
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {
        if (!this.isEditing()) {
            JDialogPathologyByPatient form = (JDialogPathologyByPatient) this.editForm;
            if ((form.getCmbPathologies().getSelectedIndex() == -1) || (form.getCmbPathologies().getSelectedItem() == null)) {
                return "Debe seleccionar una patología";
            }
            try {
                Pathology pathology = (Pathology) form.getCmbPathologies().getSelectedItem();
                if (this.facadeDB.getPathologyByPatienBroker().existPathologyByPatient(pathology, this.patient)) {
                    return "Ya existe la patología para el paciente";
                }
            } catch (ExceptionDAO ex) {
                this.facadeDB.refreshSession();
                return ex.getMessage();
            }
        }
        return null;
    }

    @Override
    protected void loadIconOnForm() {
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
        return new PathologyPatient(this.patient);
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogPathologyByPatient(parent, this);
    }

    @Override
    protected void initFormsControls() throws ExceptionDAO {

        JDialogPathologyByPatient form = (JDialogPathologyByPatient) this.editForm;
        form.getCmbPathologies().removeAllItems();
        List<Pathology> list = this.facadeDB.getPathologyBroker().getAllPathologies();
        Iterator<Pathology> it = list.iterator();
        while (it.hasNext()) {
            form.getCmbPathologies().addItem(it.next());
        }
        form.getCmbPathologies().setEnabled(!this.isEditing());
        this.editForm = form;
    }
}
